﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AAA.TradeLanguage.Data;

namespace AAA.TradeLanguage
{
    public class PerformanceAnalyzer
    {
        private PerformanceSummary _allTrade;

        public PerformanceSummary AllTrade
        {
            get { return _allTrade; }
            set { _allTrade = value; }
        }
        private PerformanceSummary _longTrade;

        public PerformanceSummary LongTrade
        {
            get { return _longTrade; }
            set { _longTrade = value; }
        }
        private PerformanceSummary _shortTrade;

        public PerformanceSummary ShortTrade
        {
            get { return _shortTrade; }
            set { _shortTrade = value; }
        }

        private float _fPointMultiple;

        public float PointMultiple
        {
            get { return _fPointMultiple; }
            set { _fPointMultiple = value; }
        }

        private float _fTaxRate;

        public float TaxRate
        {
            get { return _fTaxRate; }
            set { _fTaxRate = value; }
        }

        private float _fCommission;

        public float Commission
        {
            get { return _fCommission; }
            set { _fCommission = value; }
        }

        public PerformanceAnalyzer()
        {
            ClearPerformanceSummary();
        }

        private void ClearPerformanceSummary()
        {
            _allTrade = null;
            _longTrade = null;
            _shortTrade = null;
            _allTrade = new PerformanceSummary();
            _longTrade = new PerformanceSummary();
            _shortTrade = new PerformanceSummary();
        }

        private void SummaryTrade(SignalRecord signalRecord, PerformanceSummary performanceSummary)
        {
            float fProfit;
            float fTradeProfit;
            try
            {
                fProfit = (float)signalRecord.Profit;
                performanceSummary.TotalNetProfit += fProfit;
                performanceSummary.TotalTradeCount += 1;

                if (fProfit >= 0)
                {
                    performanceSummary.GrossProfit += fProfit;
                    performanceSummary.WinningTradeCount += 1;
                    performanceSummary.ConsecLoser = 0;
                    performanceSummary.ConsecWinner += 1;
                    if (fProfit > performanceSummary.LargestWinningTrade)
                        performanceSummary.LargestWinningTrade = fProfit;

                    if (performanceSummary.ConsecWinner > performanceSummary.MaxConsecWinner)
                        performanceSummary.MaxConsecWinner = performanceSummary.ConsecWinner;
                }
                else
                {
                    performanceSummary.GrossLoss += fProfit;
                    performanceSummary.LosingTradeCount += 1;
                    performanceSummary.ConsecWinner = 0;
                    performanceSummary.ConsecLoser += 1;
                    if (fProfit < performanceSummary.LargestLosingTrade)
                        performanceSummary.LargestLosingTrade = fProfit;

                    if (performanceSummary.ConsecLoser > performanceSummary.MaxConsecLoser)
                        performanceSummary.MaxConsecLoser = performanceSummary.ConsecLoser;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void ParseRecord(List<SignalRecord> lstSignalRecord)
        {
            SignalRecord signalRecord;
            
            try
            {
                ClearPerformanceSummary();
                for (int i = 0; i < lstSignalRecord.Count; i++)
                {
                    signalRecord = lstSignalRecord[i];
                    signalRecord.TaxRate = TaxRate;
                    signalRecord.Commission = Commission;
                    signalRecord.PointMultiple = PointMultiple;
                    SummaryTrade(signalRecord, _allTrade);
/*
                    fProfit = (float)signalRecord.Profit;
                    _allTrade.TotalNetProfit += fProfit;
                    _allTrade.TotalTradeCount += 1;

                    if (fProfit >= 0)
                    {
                        _allTrade.GrossProfit += fProfit;
                        _allTrade.WinningTradeCount += 1;
                        _allTrade.ConsecLoser = 0;
                        _allTrade.ConsecWinner += 1;

                        if (_allTrade.ConsecWinner > _allTrade.MaxConsecWinner)
                            _allTrade.MaxConsecWinner = _allTrade.ConsecWinner;
                    }
                    else
                    {
                        _allTrade.GrossLoss += fProfit;
                        _allTrade.LosingTradeCount += 1;
                        _allTrade.ConsecWinner = 0;
                        _allTrade.ConsecLoser += 1;

                        if (_allTrade.ConsecLoser > _allTrade.MaxConsecLoser)
                            _allTrade.MaxConsecLoser = _allTrade.ConsecLoser;
                    }
*/
                    switch (signalRecord.EntryOrderDirection)
                    {
                        case OrderDirectionEnum.LongEntry:
                            SummaryTrade(signalRecord, _longTrade);
                            break;
                        case OrderDirectionEnum.ShortEntry:
                            SummaryTrade(signalRecord, _shortTrade);
                            break;
                    }



                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
